package com.platform.usercenter.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.platform.common.core.utils.StringUtils;
import com.platform.common.security.service.TokenService;
import com.platform.usercenter.domain.dto.ColInfoDTO;
import com.platform.usercenter.domain.dto.SysUserCustomDTO;
import com.platform.usercenter.domain.dto.ThemeDTO;
import com.platform.usercenter.domain.vo.SysUserCustomVO;
import com.platform.usercenter.mapper.SysUserCustomMapper;
import com.platform.usercenter.service.ISysUserCustomService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import springfox.documentation.spring.web.json.Json;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class  ISysUserCustomServiceImpl implements ISysUserCustomService {

    @Resource
    private TokenService tokenService;

    @Resource
    private SysUserCustomMapper sysUserCustomMapper;

    @Override
    public SysUserCustomVO queryUserCustomSetting() {
        Long userid = tokenService.getLoginUser().getUserid();
        String userSetting = sysUserCustomMapper.queryUserCustomSetting(userid);
        if (StringUtils.isEmpty(userSetting)){
            return new SysUserCustomVO();
        }else {
            return JSONObject.parseObject(userSetting, SysUserCustomVO.class);
        }
    }

    @Override
    public Integer saveColSetting(SysUserCustomDTO colInfo) {
        Integer infectRows = 0;
        Long userid = tokenService.getLoginUser().getUserid();
        String userSetting = sysUserCustomMapper.queryUserCustomSetting(userid);
        String key = colInfo.getPageCode();
        List<ColInfoDTO> colInfoDTOList = colInfo.getColInfo();
        if (StringUtils.isEmpty(userSetting)){
            //如果为空直接插入
            SysUserCustomVO userCustomVO = new SysUserCustomVO();
            Map<String, List<ColInfoDTO>> column = new HashMap<>();
            column.put(key,colInfoDTOList);
            userCustomVO.setColumn(column);
            SerializerFeature[] feature= {SerializerFeature.WriteMapNullValue, SerializerFeature.WriteNullStringAsEmpty, SerializerFeature.WriteNullNumberAsZero};
            String jsonString = JSON.toJSONString(userCustomVO,feature);
            //重新入库
            infectRows = sysUserCustomMapper.modifyUserCustomSetting(jsonString,userid);
        }else {
            //如果不为空，则插入或替换
            SysUserCustomVO userCustomVO = JSONObject.parseObject(userSetting, SysUserCustomVO.class);
            Map<String, List<ColInfoDTO>> column = userCustomVO.getColumn();
            if (column == null || column.size() == 0){
                column = new HashMap<>();
            }
            column.put(key,colInfoDTOList);
            userCustomVO.setColumn(column);
            SerializerFeature[] feature= {SerializerFeature.WriteMapNullValue, SerializerFeature.WriteNullStringAsEmpty, SerializerFeature.WriteNullNumberAsZero};
            String jsonString = JSON.toJSONString(userCustomVO,feature);
            //重新入库
            infectRows = sysUserCustomMapper.modifyUserCustomSetting(jsonString,userid);
        }
        return infectRows;
    }

    @Override
    public Integer saveThemeSetting(ThemeDTO themeDTO) {
        Integer infectRows = 0;
        Long userid = tokenService.getLoginUser().getUserid();
        String userSetting = sysUserCustomMapper.queryUserCustomSetting(userid);
        if (StringUtils.isEmpty(userSetting)){
            //如果为空直接插入
            SysUserCustomVO sysUserCustomVO = new SysUserCustomVO();
            sysUserCustomVO.setTheme(themeDTO);
            SerializerFeature[] feature= {SerializerFeature.WriteMapNullValue, SerializerFeature.WriteNullStringAsEmpty, SerializerFeature.WriteNullNumberAsZero};
            String jsonString = JSON.toJSONString(sysUserCustomVO,feature);
            //重新入库
            infectRows = sysUserCustomMapper.modifyUserCustomSetting(jsonString,userid);
        }else {
            //如果不为空，则插入或替换
            SysUserCustomVO userCustomVO = JSONObject.parseObject(userSetting, SysUserCustomVO.class);
            userCustomVO.setTheme(themeDTO);
            SerializerFeature[] feature= {SerializerFeature.WriteMapNullValue, SerializerFeature.WriteNullStringAsEmpty, SerializerFeature.WriteNullNumberAsZero};
            String jsonString = JSON.toJSONString(userCustomVO,feature);
            //重新入库
            infectRows = sysUserCustomMapper.modifyUserCustomSetting(jsonString,userid);
        }
        return infectRows;
    }
}
